﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Code.Models;
using Code.Commen;
using Code.BLL;
using Company.Web.Models.NewsModels;

namespace Company.Web.Controllers
{
    public class NewsController : ControllerBase
    {
        NewsManage NM = new NewsManage();
        public ActionResult Index(IndexModel model)
        {
            if(model==null)
                model=new IndexModel();
            if (model.A < 1)
                model.A = null;
            if (model.B < 1)
                model.B = null;

            var page = model.A ?? 1;
            var size = model.B ?? 10;

            var data = NM.GetTable<New>().Where(p => p.Language == (short)model.L && p.State == (short)NewState.Enabled).Select(p => new NewItem { ID = p.ID, Title = p.Title, Content = p.Content, AddTime = p.AddTime, ClickCount = p.ClickCount, Highlight = p.Highlight, Language = (Language)p.Language, State = (NewState)p.State });

            model.SumCount = data.Count();
            model.News = data.OrderByDescending(p=>p.AddTime).Skip(size * (page - 1)).Take(size).ToArray();

            return View("Index_" + model.L, model);
        }

        public ActionResult Detail(Language l,long a)
        {
            var item = NM.GetTable<New>().Select(p => new NewItem { ID = p.ID, Title = p.Title, Content = p.Content, AddTime = p.AddTime, ClickCount = p.ClickCount, Highlight = p.Highlight, Language = (Language)p.Language, State = (NewState)p.State })
                .FirstOrDefault(p => p.ID == a && p.Language == l && p.State == NewState.Enabled);
            if (item == null)
                return RedirectToAction("index");
            var model = new DetailModel
            {
                L = l,
                New = item,
            };
            return View("Detail_"+l,model);
        }
    }
}
